package day17.zhiyou100;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

// 对象序列化
public class Demo08_ObjectStream {

    // ObjectInputStream 对象反序列化
    public static void m2() throws Exception {
        File file = new File("D:\\aa\\bb\\pe.txt");
        FileInputStream fis = new FileInputStream(file);
        // ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(fis);
        // 从流读取对象信息
        List<People> peoples = (List<People>)ois.readObject();
        System.out.println(peoples);//
    }

    // ObjectOutputStream  对象字节输出流
    public static void m1() throws Exception {
        // 准备一个对象
        People pe = new People("张三");
        People pe2 = new People("李四");
        List<People> peoples = new ArrayList<>();
        peoples.add(pe);
        peoples.add(pe2);
        // 假如我们还在D:\\aa\\bb\\pe.txt文件中
        File file = new File("D:\\aa\\bb\\pe.txt");
        // 准备文件字节输出流
        FileOutputStream fos = new FileOutputStream(file);
        // 创建对象字节输出流
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //NotSerializableException: com.zhiyou100.People
       /* oos.writeObject(pe);// 对象序列化
        oos.writeObject(pe2);// 对象序列化*/
        oos.writeObject(peoples);
        // 关闭流对象
        oos.close();
        fos.close();
    }


    // ObjectStream 对象序列化和反序列化注意问题
    public static void m3() throws  Exception{
        People pe = new People("张三");
        People.classroom = "321教室";
        File file = new File("D:\\aa\\bb\\obj.txt");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pe);
        oos.close();
        fos.close();
    }

    // 把文件中的对象数据取出来
    public static void m4() throws Exception {
        File file = new File("D:\\aa\\bb\\obj.txt");
        FileInputStream fis = new FileInputStream(file);
        // ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();// 对象
        System.out.println(o);// 张三
    }

    public static void main(String[] args) throws Exception {
        m3();
    }

}

// 不同类中
class Main {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\aa\\bb\\obj.txt");
        FileInputStream fis = new FileInputStream(file);
        // ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();// 对象
        System.out.println(o);// 张三

    }
}


// 自定义People
class People implements Serializable {

    // 保证你不管怎么修改该class，该class被序列化时分配的序列化版本号始终是自己指定的值
    // serialVersionUID判定在序列化过程中该类有没有被二次修改过
    private static final long serialVersionUID = 1L;

    private String name;
    private int id;
    // 静态成员变量不参与序列化
    public static String classroom = "204教室";
    // 瞬时成员变量也不参与序列化
    public transient double money = 100.0D;

    public People() {
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ",classroom='" + classroom + '\'' +
                ",money='" + money + '\'' +
                '}';
    }

    public People(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}